package com.hlbbcsr.dao.impl;

import java.util.List;

import org.hibernate.Query;

import com.hlbbcsr.dao.CustomerDAO;
import com.hlbbcsr.domain.bean.Customer;

public class CustomerDAOImpl extends BaseHelperDAO implements CustomerDAO {

	public static final String CUSTOMER_NAME	= "customerName";
	public static final String LOGIN_ID 	 	= "loginId";

	public List<Customer> findByCustomerName(Object customerName) throws Exception {
		return findByProperty(CUSTOMER_NAME, customerName);
	}
	
	public List<Customer> findByLoginId(Object loginId) throws Exception {
		return findByProperty(LOGIN_ID, loginId);
	}
	
	@SuppressWarnings("unchecked")
	public List<Customer> findByProperty(String propertyName, Object value) throws Exception {

		try {

			String queryString = "from Customer as model where model."
					+ propertyName + "= :propertyName";
			Query query = getCurrentSession().createQuery(queryString);
			query.setParameter("propertyName", value);
			return query.list();
		} catch (RuntimeException re) {
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<Customer> listCgsCustomer() throws Exception {
		List<Customer> result = null;
		
		String queryString = "from Customer"; // Hibernate Query Language
		result = getCurrentSession().createQuery(queryString).list();
		
		return result;
	}
	
	/**
	 * Insert customer into database
	 */
	public void addCustomer(Customer entity) throws Exception {
		
		// Insert into database
		getCurrentSession().save(entity);
		
	}
}